'use strict';

const { Controller } = require('egg');

class MainController extends Controller {
    // 判断用户名密码是否正确
    async checkLogin() {
        const { ctx } = this;
        let userName = ctx.request.body.userName;
        let password = ctx.request.body.password;
        const sql = " SELECT userName FROM admin_user WHERE userName='" + userName + "' AND password = '" + password + "'";

        const res = await this.app.mysql.query(sql);

        if (res.length > 0) {
            // 登录成功,进行session缓存
            let openId = new Date().getTime();
            ctx.session.openId = openId;
            ctx.body = { code: 0, msg: "登录成功", data: { openId } }
        } else {
            ctx.body = { code: 1, msg: '登录失败' }
        }
    }

    // 后台文章分类信息
    async getTypeInfo() {
        const resType = await this.app.mysql.select('type');
        this.ctx.body = { code: 0, msg: null, data: resType }
    }

    // 添加文章
    async addArticle() {
        let tmpArticle = this.ctx.request.body;

        const result = await this.app.mysql.insert('article', tmpArticle);
        const insertSuccess = result.affectedRows === 1;
        const insertId = result.insertId;

        this.ctx.body = {
            code: insertSuccess ? 0 : 1,
            msg: insertSuccess ? "添加文章成功" : "添加文章失败",
            data: {
                insertId
            }
        }
    }

    // 修改文章
    async updateArticle() {
        let tempArticle = this.ctx.request.body;

        const result = await this.app.mysql.update('article', tempArticle);
        const updateSuccess = result.affectedRows === 1;

        this.ctx.body = {
            code: updateSuccess ? 0 : 1,
            msg: updateSuccess ? "修改文章成功" : "修改文章成功"
        }
    }

    // 获得文章列表
    async getArticleList() {
        let sql = 'SELECT article.id as id,' +
            'article.title as title,' +
            'article.introduce as introduce,' +
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime," +
            'article.view_count as view_count ,' +
            'type.typeName as typeName ' +
            'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
            'ORDER BY article.id DESC ';

        const result = await this.app.mysql.query(sql);

        this.ctx.body = {
            code: 0,
            msg: null,
            data: {
                list: result
            }
        }
    }

    // 删除文章
    async delArticle() {
        let id = this.ctx.params.id;
        await this.app.mysql.delete("article", { id });

        this.ctx.body = {
            code: 0,
            msg: "删除成功"
        }
    }

    // 根据文章ID得到文章详情，用于修改文章
    async getArticleById() {
        let id = this.ctx.params.id;

        let sql = 'SELECT article.id as id,' +
            'article.title as title,' +
            'article.introduce as introduce,' +
            'article.article_content as article_content,' +
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime," +
            'article.view_count as view_count ,' +
            'type.typeName as typeName, ' +
            'type.id as typeId ' +
            'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
            'WHERE article.id=' + id;

        let data = await this.app.mysql.query(sql);

        this.ctx.body = {
            code: 0,
            msg: null,
            data
        }
    }
}

module.exports = MainController;